import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
import random
import logging

#<!-- Class user -->

class Account(db.Model):
  personName = db.StringProperty(required=True)
  accountName = db.StringProperty(required=True)
  password = db.StringProperty(required=True)
  adminStatus = db.BooleanProperty(required=True)

def CreateAccount(personName, accountName, password, adminStatus):
  account = Account(personName=personName, accountName=accountName, password=password, adminStatus=adminStatus)
  account.put()

class Project(db.Model):
  projectName = db.StringProperty(required=True)

def CreateProject(projectName): 
  project = Project(projectName=projectName)
  project.put()

class Task(db.Model):
  nameOfTask = db.StringProperty(required=True) 
  details = db.StringProperty(required=True)
  concerns = db.StringProperty(required=False)
  listOfWorkers = db.StringListProperty(required=True)
  project = db.ReferenceProperty(Project, collection_name = 'tasks')
# Create query, filter out tasks related to a category or project.

def CreateTask(nameOfTask, details, listOfWorkers): 
  task = Task(nameOfTask=nameOfTask, details=details, listOfWorkers=listOfWorkers)
  Task.put(task)
  

class LoginScreenHandler(webapp.RequestHandler):
   def get(self):
     path = os.path.join(os.path.dirname(__file__), 'reshope', 'login.html')

     self.response.out.write(template.render(path, {}))

   def post(self): 
     inputusername = self.request.get('username')
     inputpassword = self.request.get('password')
     query = Account.all()
     query.filter("accountName =", inputusername)
       

     if (query.get() != None):
        if (query.get().accountName == inputusername) and (query.get().password == inputpassword): 
           self.redirect("/user")

     path = os.path.join(os.path.dirname(__file__), 'reshope', 'login.html')
    
     self.response.out.write(template.render(path, {}))



class UserProfileHandler(webapp.RequestHandler):
   def get(self):
     template_values = {'projectlist': Project.all()}
     path = os.path.join(os.path.dirname(__file__), 'reshope', 'user.html')
#     logging.info(path)
     self.response.out.write(template.render(path, {}))   

   def post(self):
     nameofproject = self.request.get('inputprojectname')
     CreateProject(nameofproject)
     template_values = {'projectlist': Project.all()}
     path = os.path.join(os.path.dirname(__file__), 'reshope', 'user.html')
     self.response.out.write(template.render(path, template_values)) 

class RegisterSiteHandler(webapp.RequestHandler):
   def get(self):
     path = os.path.join(os.path.dirname(__file__), 'reshope', 'register.html')
     self.response.out.write(template.render(path, {})) 
     logging.info('Hit Register def Get ====================')  

   def post(self):
     reg_name = self.request.get('name')
     reg_accountname = self.request.get('username')
     reg_password = self.request.get('password')
     reg_confpassword = self.request.get('confirmpassword')

     CreateAccount(reg_name, reg_accountname, reg_password, False)


     path = os.path.join(os.path.dirname(__file__), 'reshope', 'register.html')
     self.response.out.write(template.render(path, {})) 
     logging.info("Hit Register def Post ===================")
     self.redirect("/login")

class ProjectSiteHandler(webapp.RequestHandler):
  def get(self):
    template_values = {'tasklist': Task.all()} 
    path = os.path.join(os.path.dirname(__file__), 'reshope', 'project.html')
    self.response.out.write(template.render(path, template_values))  

  def post(self):

    nameoftask = self.request.get('inputtaskname')
    CreateTask(nameoftask, "details", list('d'))
    template_values = {'tasklist': Task.all()}
    path = os.path.join(os.path.dirname(__file__), 'reshope', 'project.html')
    self.response.out.write(template.render(path, template_values))  

class VolunteerSiteHandler(webapp.RequestHandler):
   def get(self):
     path = os.path.join(os.path.dirname(__file__), 'reshope', 'volunteer.html')
     self.response.out.write(template.render(path, {}))  

   def post(self):
     path = os.path.join(os.path.dirname(__file__), 'reshope', 'volunteer.html')
     self.response.out.write(template.render(path, {}))     
     self.redirect('project')

class AssignSiteHandler(webapp.RequestHandler):
   def get(self):
     getTaskID = self.request.get('taskid')
     logging.info(getTaskID)
     if (Task.all().filter("taskKey =", getTaskID).get() == None):
        Workers = list()
     else: 
        Workers = Task.all().filter("taskKey =", getTaskID).get().listOfWorkers
     template_values = {'TaskID': getTaskID, 'workerlist': Workers, 'accountlist': Account.all()}
     path = os.path.join(os.path.dirname(__file__), 'reshope', 'assign.html')
     self.response.out.write(template.render(path, template_values))      

   def post(self):
     username = self.request.get('accountuser')
     getTaskID = self.request.get('TaskID')
     logging.info(getTaskID)
     query = Account.all()
     query.filter("accountName =", username)
     if (Task.all().filter("taskKey =", getTaskID).get() == None):
        Workers = list()
     else: 
        Workers = Task.all().filter("taskKey =", getTaskID).get().listOfWorkers
     TaskID = self.request.get('TaskID')
     if username:
        task = Task.get(TaskID)
        if task: 
           task.listOfWorkers.append(username)

     template_values = {'TaskID': getTaskID, 'workerlist': Workers, 'accountlist': Account.all()}

     path = os.path.join(os.path.dirname(__file__), 'reshope', 'assign.html')
     self.response.out.write(template.render(path, template_values))      


#David needs to Get Done


class TaskDetailsSiteHandler(webapp.RequestHandler):
   def get(self):

     task_to_display = self.request.get('taskid') 
     query = Task.all()
     query.filter('nameOfTask =', task_to_display)
     for tasks in query:
        tasks.details 
 
     template_values = {'Task_Details': query.details}
     path = os.path.join(os.path.dirname(__file__), 'reshope', 'taskdetail.html')
     self.response.out.write(template.render(path,template_values))
         
    
#David needs to Get Done

class TaskEditSiteHandler(webapp.RequestHandler):
  def get(self):
     
    path = os.path.join(os.path.dirname(__file__), 'reshope', 'taskedit.html')
    self.response.out.write(template.render(path, {}))    
    task_to_edit = self.request.get('taskid')
    classleveltask = task_to_edit
    query = Task.all()
    query.filter('nameOfTask =', task_to_edit) 
   
  def post(self):
    
    Details_to_save = self.request.get('/project_button')
    query = Task.all()
    query.filter('nameOfTask =', classleveltask)
    
    for tasks in query:
        tasks.details = Details_to_save
                  
#David needs to Get Done
 

class DeleteButtonHandler(webapp.RequestHandler):
  def get(self): 
    task_to_delete = self.request.get('taskid')
    Task.get(task_to_delete).delete()
    self.redirect('/project')

     

def main():
  application = webapp.WSGIApplication([('/taskedit.*', TaskEditSiteHandler),
                                        ('/taskdetail.*', TaskDetailsSiteHandler),
                                        ('/assign.*', AssignSiteHandler), 
                                        ('/volunteer.*', VolunteerSiteHandler), 
                                        ('/project.*', ProjectSiteHandler),
                                        ('/register.*', RegisterSiteHandler),
                                        ('/user.*', UserProfileHandler),
                                        ('/delete.*', DeleteButtonHandler),
                                        ('/.*', LoginScreenHandler)],s
                                       debug=True)
  run_wsgi_app(application)

if __name__ == '__main__':
  main()
